namespace DotNetNuke.UI.Skins
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Services.Localization;
    using ICSharpCode.SharpZipLib.Zip;
    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.IO;

    /// -----------------------------------------------------------------------------
    /// Project	 : DotNetNuke
    /// Class	 : SkinController
    /// 
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// Handles the Business Control Layer for Skins
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [willhsc]	3/3/2004	Created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class SkinController
    {
        public static string FormatMessage(string Title, string Body, int Level, bool IsError)
        {
            string Message = Title;
            if (IsError)
            {
                Message = "<font class=\"NormalRed\">" + Title + "</font>";
            }
            switch (Level)
            {
                case -1:
                    Message = "<hr><br><b>" + Message + "</b>";
                    break;

                case 0:
                    Message = "<br><br><b>" + Message + "</b>";
                    break;

                case 1:
                    Message = "<br><b>" + Message + "</b>";
                    break;

                default:
                    Message = "<br><li>" + Message;
                    break;
            }
            return (Message + ": " + Body + "\r\n");
        }

        public static string FormatSkinPath(string SkinSrc)
        {
            string strSkinSrc = SkinSrc;
            if (strSkinSrc != "")
            {
                strSkinSrc = Strings.Left(strSkinSrc, Strings.InStrRev(strSkinSrc, "/", -1, CompareMethod.Binary));
            }
            return strSkinSrc;
        }

        public static string FormatSkinSrc(string SkinSrc, PortalSettings PortalSettings)
        {
            string strSkinSrc = SkinSrc;
            if (strSkinSrc != "")
            {
                switch (strSkinSrc.Substring(0, 3))
                {
                    case "[G]":
                        return strSkinSrc.Replace("[G]", DotNetNuke.Common.Globals.HostPath);

                    case "[L]":
                        strSkinSrc = strSkinSrc.Replace("[L]", PortalSettings.HomeDirectory);
                        break;
                }
            }
            return strSkinSrc;
        }

        public static SkinInfo GetSkin(string SkinRoot, int PortalId, SkinType SkinType)
        {
            return (SkinInfo) CBO.FillObject(DataProvider.Instance().GetSkin(SkinRoot, PortalId, (int) SkinType), typeof(SkinInfo));
        }

        public static void SetSkin(string SkinRoot, int PortalId, SkinType SkinType, string SkinSrc)
        {
            DataProvider.Instance().DeleteSkin(SkinRoot, PortalId, (int) SkinType);
            if (SkinSrc != "")
            {
                DataProvider.Instance().AddSkin(SkinRoot, PortalId, (int) SkinType, SkinSrc);
            }
        }

        public static string UploadSkin(string RootPath, string SkinRoot, string SkinName, Stream objInputStream)
        {
            ZipInputStream objZipInputStream = new ZipInputStream(objInputStream);
            int intSize = 0x800;
            byte[] arrData = new byte[0x801];
            string strMessage = "";
            ArrayList arrSkinFiles = new ArrayList();
            PortalSettings ResourcePortalSettings = DotNetNuke.Common.Globals.GetPortalSettings();
            string BEGIN_MESSAGE = DotNetNuke.Services.Localization.Localization.GetString("BeginZip", ResourcePortalSettings);
            string CREATE_DIR = DotNetNuke.Services.Localization.Localization.GetString("CreateDir", ResourcePortalSettings);
            string WRITE_FILE = DotNetNuke.Services.Localization.Localization.GetString("WriteFile", ResourcePortalSettings);
            string FILE_ERROR = DotNetNuke.Services.Localization.Localization.GetString("FileError", ResourcePortalSettings);
            string END_MESSAGE = DotNetNuke.Services.Localization.Localization.GetString("EndZip", ResourcePortalSettings);
            string FILE_RESTICTED = DotNetNuke.Services.Localization.Localization.GetString("FileRestricted", ResourcePortalSettings);
            strMessage = strMessage + FormatMessage(BEGIN_MESSAGE, SkinName, -1, false);
            for (ZipEntry objZipEntry = objZipInputStream.GetNextEntry(); objZipEntry != null; objZipEntry = objZipInputStream.GetNextEntry())
            {
                if (!objZipEntry.IsDirectory)
                {
                    string strExtension = objZipEntry.Name.Substring(objZipEntry.Name.LastIndexOf(".") + 1);
                    if (Strings.InStr(1, ",ASCX,HTM,HTML,CSS,SWF,RESX," + DotNetNuke.Common.Globals.HostSettings["FileExtensions"].ToString().ToUpper(), "," + strExtension.ToUpper(), CompareMethod.Binary) != 0)
                    {
                        string refStringHelperL0 = objZipEntry.Name.ToLower();
                        if (refStringHelperL0 == (SkinInfo.RootSkin.ToLower() + ".zip"))
                        {
                            MemoryStream objMemoryStream = new MemoryStream();
                            intSize = objZipInputStream.Read(arrData, 0, arrData.Length);
                            while (intSize > 0)
                            {
                                objMemoryStream.Write(arrData, 0, intSize);
                                intSize = objZipInputStream.Read(arrData, 0, arrData.Length);
                            }
                            objMemoryStream.Seek(0L, SeekOrigin.Begin);
                            strMessage = strMessage + UploadSkin(RootPath, SkinInfo.RootSkin, SkinName, objMemoryStream);
                        }
                        else if (refStringHelperL0 == (SkinInfo.RootContainer.ToLower() + ".zip"))
                        {
                            MemoryStream objMemoryStream = new MemoryStream();
                            intSize = objZipInputStream.Read(arrData, 0, arrData.Length);
                            while (intSize > 0)
                            {
                                objMemoryStream.Write(arrData, 0, intSize);
                                intSize = objZipInputStream.Read(arrData, 0, arrData.Length);
                            }
                            objMemoryStream.Seek(0L, SeekOrigin.Begin);
                            strMessage = strMessage + UploadSkin(RootPath, SkinInfo.RootContainer, SkinName, objMemoryStream);
                        }
                        else
                        {
                            string strFileName = RootPath + SkinRoot + @"\" + SkinName + @"\" + objZipEntry.Name;
                            if (!Directory.Exists(Path.GetDirectoryName(strFileName)))
                            {
                                strMessage = strMessage + FormatMessage(CREATE_DIR, Path.GetDirectoryName(strFileName), 2, false);
                                Directory.CreateDirectory(Path.GetDirectoryName(strFileName));
                            }
                            if (File.Exists(strFileName))
                            {
                                File.SetAttributes(strFileName, FileAttributes.Normal);
                                File.Delete(strFileName);
                            }
                            FileStream objFileStream = File.Create(strFileName);
                            strMessage = strMessage + FormatMessage(WRITE_FILE, Path.GetFileName(strFileName), 2, false);
                            for (intSize = objZipInputStream.Read(arrData, 0, arrData.Length); intSize > 0; intSize = objZipInputStream.Read(arrData, 0, arrData.Length))
                            {
                                objFileStream.Write(arrData, 0, intSize);
                            }
                            objFileStream.Close();
                            string refStringHelperL1 = Path.GetExtension(strFileName);
                            if (((refStringHelperL1 == ".htm") || (refStringHelperL1 == ".html")) || (refStringHelperL1 == ".ascx"))
                            {
                            }
                            if ((((refStringHelperL1 == ".css") ? 1 : 0) != 0) && (strFileName.ToLower().IndexOf("about.htm".ToLower()) < 0))
                            {
                                arrSkinFiles.Add(strFileName);
                            }
                        }
                    }
                    else
                    {
                        strMessage = strMessage + FormatMessage(FILE_ERROR, string.Format(FILE_RESTICTED, objZipEntry.Name, Strings.Replace(DotNetNuke.Common.Globals.HostSettings["FileExtensions"].ToString(), ",", ", *.", 1, -1, CompareMethod.Binary)), 2, true);
                    }
                }
            }
            strMessage = strMessage + FormatMessage(END_MESSAGE, SkinName + ".zip", 1, false);
            objZipInputStream.Close();
            SkinFileProcessor NewSkin = new SkinFileProcessor(RootPath, SkinRoot, SkinName);
            return (strMessage + NewSkin.ProcessList(arrSkinFiles, SkinParser.Portable));
        }

        public static string UploadSkin(string RootPath, string SkinRoot, string SkinName, string Path)
        {
            string strMessage = "";
            FileStream objFileStream = new FileStream(Path, FileMode.Open, FileAccess.Read);
            strMessage = UploadSkin(RootPath, SkinRoot, SkinName, objFileStream);
            objFileStream.Close();
            return strMessage;
        }
    }
}

